package cn.hx.controller.system;

import cn.hx.entity.Item;
import cn.hx.entity.ItemUser;
import cn.hx.service.ItemService;
import cn.hx.service.ItemUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class itemUserController {


    @Resource
    private ItemUserService itemUserService;
    @Resource
    private ItemService itemService;



    @GetMapping("system/item/item_user")
    private String groupList(ItemUser itemUser,
                             ModelMap modelMap,
                             @RequestParam(defaultValue = "1") int page,
                             @RequestParam(defaultValue = "10") int size){
        modelMap.addAttribute("itemUser",itemUserService.page(itemUser,page, size));
        return "system/item/item_user";
    }
    @GetMapping("system/item/item_user/insert")
    private String insert(ModelMap modelMap ){

        modelMap.addAttribute("itemUser",itemService.queryAllByLimit(new Item(),0,2000));
        System.out.println(itemService.queryAllByLimit(new Item(),0,1000));
        return "system/item/item_user_save";

    }
    @Value("${web.upload-path}")
    private String filePath;
    @PostMapping("system/item/item_user/save")
    private String save(ItemUser itemUser, MultipartFile file) throws IOException {
        if (file != null && !file.isEmpty()) {

            String filename = file.getOriginalFilename();
            String type = filename.substring(filename.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
//            file.transferTo(new File("D:\\file\\" + uuid + type));
//            itemGroup.setIcon(uuid + type);
            File file1 = new File(filePath + "/" + type);
            if (!file1.isDirectory()){
                file1.mkdirs();
            }
            file.transferTo(new File(filePath+"/" +type+"/"+ uuid + type));
            itemUser.setHeadPortrait("/"+type+"/" + uuid + type);
        }
        itemUserService.insert(itemUser);
        return "redirect:/system/item/item_user";
    }
    @GetMapping("system/item/item_user/update/{id}")
    private String update(@PathVariable Integer id,ModelMap modelMap){
        modelMap.addAttribute("itemUser",itemUserService.queryById(id));
        return "system/item/item_user_update";

    }

    @PostMapping("system/item/item_user/update/change/{id}")
    private String update(ItemUser itemUser, MultipartFile file) throws IOException {
        if (file != null && !file.isEmpty()) {

            String filename = file.getOriginalFilename();
            String type = filename.substring(filename.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
//            file.transferTo(new File("D:\\file\\" + uuid + type));
//            itemGroup.setIcon(uuid + type);
            File file1 = new File(filePath + "/" + type);
            if (!file1.isDirectory()){
                file1.mkdirs();
            }
            file.transferTo(new File(filePath+"/" +type+"/"+ uuid + type));
            itemUser.setHeadPortrait("/"+type+"/" + uuid + type);
        }
        itemUserService.update(itemUser);
        return "redirect:/system/item/item_user";
    }
    @GetMapping("system/item/item_user/del/{id}")
    public String del(@PathVariable Integer id){
        itemUserService.deleteById(id);
        return "redirect:/system/item/item_user";
    }
}
